/*
 * Copyright (c) 2019 Author: Fancyears Milos Malvis @1243093366@qq.com
 * ALL COPYRIGHT REVERSED.
 */

package com.fancyears.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.fancyears.encrypt.RSAUtils;
import com.fancyears.global.GlobalSetting;
import com.fancyears.redis.client.RedisClientDao;
import com.fancyears.redis.bean.UserSidBean;
import com.fancyears.service.i.encryption.RSAService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: Fancyears Milos Malvis
 * @Date: Created in  2019/5/22 16:34
 * @Modified By:
 */
@Slf4j
public class RedisSessionInterceptor implements HandlerInterceptor {

    @Autowired
    RSAService rsaService;
    @Autowired
    private RedisClientDao<UserSidBean> redisClientDao;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //无论访问的地址是不是正确的，都进行登录验证，登录成功后的访问再进行分发，404的访问自然会进入到错误控制器中
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute(GlobalSetting.USERNAME);
        //验证当前请求的session是否是已登录的session
        UserSidBean bean;
        if (StringUtils.isNotEmpty(username)) {
            JSONObject result = new JSONObject();
            result.put(GlobalSetting.STATUS, GlobalSetting.STATUS_ENUM.OK);
            result.put(GlobalSetting.MESSAGE, "用户已登录");
            result.put(GlobalSetting.USERNAME, username);
            session.setAttribute("precheck", result);
            return true;
        } else {
            //直接拦截掉，不会进入session了
            response401(response,session);
            //response.sendRedirect("/8081/#/index");
            return false;
        }
    }

    private void response401(HttpServletResponse response, HttpSession session) throws NoSuchAlgorithmException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
            JSONObject result = new JSONObject();
            result.put(GlobalSetting.STATUS, GlobalSetting.STATUS_ENUM.SING_OUT);
            result.put(GlobalSetting.MESSAGE, "用户未登录");
            // RSAUtils.getModulus();
            response.getWriter().print(result);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        log.warn("------postHandle--------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        log.warn("----------afterCompletion---------------");
    }

}
